package com.museum.guide.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.museum.guide.common.exception.BusinessException;
import com.museum.guide.common.result.ResultCode;
import com.museum.guide.dto.LoginDTO;
import com.museum.guide.dto.LoginResponseDTO;
import com.museum.guide.dto.RegisterDTO;
import com.museum.guide.entity.User;
import com.museum.guide.mapper.UserMapper;
import com.museum.guide.common.util.JwtUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import javax.annotation.Resource;

@Service
public class UserService extends ServiceImpl<UserMapper, User> {

    @Resource
    private JwtUtil jwtUtil;

    /**
     * 用户登录
     */
    public LoginResponseDTO login(LoginDTO loginDTO) {

        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();

        // 查询用户
        User user = lambdaQuery().eq(User::getUsername, username).one();
        if (user == null) {
            throw new BusinessException(ResultCode.USERNAME_PASSWORD_ERROR);
        }

        // 校验密码
        if (!com.baomidou.mybatisplus.core.toolkit.StringUtils.equals(password, user.getPassword())) {
            throw new BusinessException(ResultCode.USERNAME_PASSWORD_ERROR);
        }

        // 校验角色
        if (!user.getRole().equals(loginDTO.getRole())) {
            throw new BusinessException("用户角色不匹配");
        }

        // 校验状态
        if (user.getStatus() == 0) {
            throw new BusinessException(ResultCode.ACCOUNT_DISABLED);
        }

        // 生成token
        String token = jwtUtil.generateToken(user.getId(), user.getUsername(), user.getRole());

        // 构建返回对象
        LoginResponseDTO response = new LoginResponseDTO();
        response.setToken(token);

        response.setUserInfo(user);

        return response;
    }

    /**
     * 用户注册
     */
    public void register(RegisterDTO registerDTO) {
        // 校验两次密码是否一致
        if (!registerDTO.getPassword().equals(registerDTO.getConfirmPassword())) {
            throw new BusinessException("两次密码不一致");
        }

        // 校验用户名是否存在
        Long count = lambdaQuery().eq(User::getUsername, registerDTO.getUsername()).count();
        if (count > 0) {
            throw new BusinessException(ResultCode.USERNAME_EXIST);
        }

        // 创建用户
        User user = new User();
        user.setUsername(registerDTO.getUsername());
        user.setPassword(registerDTO.getPassword());
        user.setRole(registerDTO.getRole());
        user.setRealName(registerDTO.getRealName());
        user.setPhone(registerDTO.getPhone());
        user.setEmail(registerDTO.getEmail());
        user.setStatus(1);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        save(user);
    }

    /**
     * 分页查询用户
     */
    public Page<User> page(Page<User> page, String username) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(username), User::getUsername, username);
        return page(page, wrapper);
    }

    /**
     * 新增用户
     */
    public void saveUser(User user) {
        user.setPassword(user.getPassword());
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        save(user);
    }

    /**
     * 更新用户
     */
    public void updateUser(User user) {
        user.setUpdateTime(LocalDateTime.now());
        updateById(user);
    }

    /**
     * 删除用户
     */
    public void removeUser(Long id) {
        removeById(id);
    }
}